package com.bookmarket.controller;

import com.bookmarket.entity.Address;
import com.bookmarket.entity.Custom;
import com.bookmarket.entity.Discount;
import com.bookmarket.entity.Shop;
import com.bookmarket.service.HJHStageLoginAndRegService;
import com.bookmarket.service.LJOrderListService;
import com.bookmarket.service.LJShopCartService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/shop")
@SessionAttributes(names="list")
public class LJShopCartController {


    @Resource
    private LJShopCartService ljShopCartService;

    @Resource
    private LJOrderListService ljOrderListService;

    @Resource
    private HJHStageLoginAndRegService hJHStageLoginAndRegService;


    @RequestMapping("/toShop")
    public ModelAndView toShop(Custom custom,HttpSession session){
        ModelAndView mv = new ModelAndView();
        Custom custom1=hJHStageLoginAndRegService.CustomLogin(custom);
        if (custom1!=null){
            session.setAttribute("NowCustom",custom1);
        }
        mv.setViewName("stage/LJ_shopCart");
        return mv;
    }

    //查询购物车
    @ResponseBody
    @RequestMapping("/chaShopByCustom")
    public ModelAndView chaShopByCustom(String name){
//        System.out.println("*****");
//        System.out.println(name);
        ModelAndView mv=new ModelAndView();
        Custom c=ljShopCartService.selectBy(name);
//        System.out.println(c.getName());
//        System.out.println("====");
        List<Shop> list=ljShopCartService.queryByCustom(c);
//        for (Shop s:list){
//            System.out.println(s.getBook_name());
//        }
        mv.addObject("list",list);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //单个删除
    @RequestMapping("/delShop")
    public ModelAndView delShop(int id){
        ModelAndView mv=new ModelAndView();
        boolean falg=ljShopCartService.delShop(id);
        mv.addObject("falg",falg);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //批量删除
    @RequestMapping("/delAllShop")
    public ModelAndView delAllShop(String shuzu){
        boolean falg=false;
        String [] sz=shuzu.split(",");
        for (int i=0;i<sz.length;i++){
            falg=ljShopCartService.delShop(Integer.parseInt(sz[i]));
        }
        ModelAndView mv=new ModelAndView();
        mv.addObject("falg",falg);
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }
    //id字符串
    @RequestMapping("/jiezhi")
    public ModelAndView jiezhi(String zifuchuan, String shuliang , float zongjia, HttpSession session){
        ModelAndView mv=new ModelAndView();
        List<Shop> list=new ArrayList<Shop>();
        String [] ids=zifuchuan.split(",");
        if (zifuchuan==""||shuliang==""||zongjia==0){
           mv.setViewName("stage/LJ_shopCart");
        }else{
            for (int i=0;i<ids.length;i++){
                Shop s=ljShopCartService.selectById(Integer.parseInt(ids[i]));
                list.add(s);
            }
            mv.addObject("list",list);
            String shopid="";
            for (Shop sh:list){
                shopid=sh.getId()+","+shopid;
            }
            mv.addObject("shopid",shopid);
            mv.addObject("zongjia",zongjia);
            mv.setViewName("stage/LJ_orders");
        }
        Custom custom = (Custom) session.getAttribute("NowCustom");
        Custom c=ljOrderListService.selectBy(custom.getName());
        List<Address> result=ljOrderListService.selectByCustom(c);
        mv.addObject("result",result);

//        查询用户已领取的优惠券
        List<Map> discounts = ljOrderListService.queryDiscount(custom.getId());
        mv.addObject("discounts",discounts);
        return mv;
    }
    public LJShopCartService getLjShopCartService() {
        return ljShopCartService;
    }

    public void setLjShopCartService(LJShopCartService ljShopCartService) {
        this.ljShopCartService = ljShopCartService;
    }
    public void setLjOrderListService(LJOrderListService ljOrderListService) {
        this.ljOrderListService = ljOrderListService;
    }
}
